package com.wynn.service.impl;


import com.wynn.dao.ProductLineDao;
import com.wynn.pojo.Equipment;
import com.wynn.pojo.ProductLine;
import com.wynn.pojo.Result;
import com.wynn.service.ProductLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Transactional
@RequestMapping("productLine")
public class ProductLineServiceImpl implements ProductLineService {

    @Autowired
    private ProductLineDao productLineDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @GetMapping("selectAll")
    public Result selectAll(){
        Result result = new Result();
        try{

            List<ProductLine> lineList = null;
            Boolean flag = redisTemplate.hasKey("lineList" );
            if (flag) {
                // 优先从缓存获取
                lineList = redisTemplate.opsForList().range("lineList", 0, -1);
            }else {
                lineList = productLineDao.findAllProductLine();
                redisTemplate.opsForList().rightPushAll("lineList", lineList);
            }

            result.setData(lineList);
            result.setCode("200");
        }catch (Exception e){
            result.setCode("503");
            result.setMessage("查询产线失败");
        }
        return result;
    }

}
